﻿Public Class frmChildSearch
    Dim m_cChildren As ListViewItem()

    Private Sub frmChildSearch_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        e.Cancel = True
        Me.Hide()
    End Sub
    Private Sub frmChildSearch_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Icon = Icon.FromHandle(My.Resources.magnifier.GetHicon)
        lvChildren.Clear()
        Me.Populate(ChildList)
    End Sub
    Sub Populate(ByRef children As Collection)
        Dim child As ChildInfo
        Dim record As ListViewItem
        Dim count As Long = 0

        imgList.Images.Clear()

        imgList.Images.Add(My.Resources.noimage)

        ReDim Me.m_cChildren(children.Count)

        For Each child In children
            imgList.Images.Add(child.ID, child.Photo)

            record = New ListViewItem
            'record.ImageIndex = child.ID
            record.ImageKey = child.ID

            record.ToolTipText = String.Format("{1}, {0}" & vbCrLf & "Gender: {2}" & vbCrLf & "Age: {3}", Arguments(child.FirstName, child.LastName, child.Gender, child.Age))
            record.Tag = child
            lvChildren.Items.Add(record)

            Me.m_cChildren(count) = record

            count += 1
        Next
    End Sub
    Sub UpdateResults()
        'Exit the function if we aren't ready yet ( setting checkboxes to start checked triggers their value changed
        'event, so this is needed ).
        If Me.m_cChildren Is Nothing Then
            Exit Sub
        End If

        'Now that we know we want to be in here, declare our variables being used in the loop
        Dim remove As Boolean
        Dim child As ChildInfo
        Dim item As ListViewItem

        For Each item In Me.m_cChildren
            '(Re)set the variables
            remove = False

            If item Is Nothing OrElse item.Tag Is Nothing Then
                'Between changes in main.vb, and updating other items, it became possible
                'For item to be null here, and by extension, it is possible item.Tag
                'can be null here as well

                'If it is, just advance to the next item
                Continue For
            End If

            child = CType(item.Tag, ChildInfo)

            'If the first name does not fit the first name box's text, mark for removal
            'If the last name does not fit the last name box's text, mark for removal
            'If the child is male and the male checkbox is unchecked, or female and female is unchecked, mark for
            ' removal
            'If the child is not the age in the age box, mark for removal

            If Not (child.FirstName Like "*" & txtFirstName.Text & "*") Then
                'If the first name does not contain the text offered, quit
                remove = True
            End If

            If Not (child.LastName Like "*" & txtLastName.Text & "*") Then
                'If the last name does not contain the text offered, quit
                remove = True
            End If

            If child.Gender.Equals(MALE) And Not chkMale.Checked Then
                'If the gender equals MALE and MALE is not checked, quit
                remove = True
            End If

            If child.Gender.Equals(FEMALE) And Not chkFemale.Checked Then
                'If the gender equals FEMALE and FEMALE is not checked, quit
                remove = True
            End If

            If IsNumeric(txtAge.Text) AndAlso child.Age <> CInt(txtAge.Text) Then
                'If the age is numeric ( valid ) and Age is not equal to it, quit
                remove = True
            End If

            'If not marked for removal, re-add if not in view, else remove from view

            If Not remove Then
                If Not lvChildren.Items.Contains(item) Then
                    'Only add it if it needs re-adding
                    lvChildren.Items.Add(item)
                End If
            Else
                item.Remove()
            End If
        Next
    End Sub
    Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtFirstName.TextChanged
        Me.UpdateResults()
    End Sub
    Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtLastName.TextChanged
        Me.UpdateResults()
    End Sub
    Private Sub TextBox4_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtAge.TextChanged
        Me.UpdateResults()
    End Sub
    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkMale.CheckedChanged
        Me.UpdateResults()
    End Sub
    Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkFemale.CheckedChanged
        Me.UpdateResults()
    End Sub
    Private Sub ListView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lvChildren.DoubleClick
        Dim details As New frmChildDatas

        details.m_cChild = CType(lvChildren.SelectedItems(0).Tag, ChildInfo)

        details.ShowDialog()
    End Sub
End Class